package com.zhang.zhangapp.view.mine;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jwenfeng.library.pulltorefresh.BaseRefreshListener;
import com.jwenfeng.library.pulltorefresh.PullToRefreshLayout;
import com.zhang.zhangapp.R;
import com.zhang.zhangapp.application.MApplication;
import com.zhang.zhangapp.base.config.BaseConfig;
import com.zhang.zhangapp.base.http.MCallback;
import com.zhang.zhangapp.bean.user.BankCard;
import com.zhang.zhangapp.bean.user.Bill;
import com.zhang.zhangapp.http.CommonHttp;
import com.zhang.zhangapp.view.BaseAppCompatActivityActivity;
import com.zhang.zhangapp.view.adapter.BankAdapter;
import com.zhang.zhangapp.view.adapter.BillsAdapter;
import com.zhang.zhangapp.view.custom.HeadRefreshView;

import org.xutils.common.Callback;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/4/25.
 */
@ContentView(R.layout.activity_mine_bills)
public class MineBankListActivity extends BaseAppCompatActivityActivity {

    @ViewInject(R.id.refresh_layout)
    PullToRefreshLayout refreshLayout;
    @ViewInject(R.id.bill_listview)
    ListView listView;
    @ViewInject(R.id.action_bar_title)
    TextView titleText;
    @ViewInject(R.id.add_bank_text)
    TextView addBankText;
    BankAdapter adapter;
    List<BankCard> cardList = new ArrayList<>();
    Map<String, String> params = new HashMap<>();

    MCallback callback = new MCallback() {
        @Override
        public void onSuccess(String data) {
            Gson gson = new Gson();
            List<BankCard> list = gson.fromJson(data, new TypeToken<List<BankCard>>() {
            }.getType());
            if (list != null)
                cardList.addAll(list);
            adapter.notifyDataSetChanged();
        }

        @Override
        public void onSuccess(String message, String code) {

        }

        @Override
        public void onError(Throwable ex, boolean isOnCallback) {

        }

        @Override
        public void onCancelled(Callback.CancelledException cex) {

        }

        @Override
        public void onFinished() {
            dismisionDalog();
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        initView();
        getCard();
    }
    @Event(type = View.OnClickListener.class,value = R.id.action_bar_left_image)
    private void back(View view){
        this.finish();
    }
    private void initView() {
        titleText.setText("我的银行卡");
        addBankText.setVisibility(View.VISIBLE);
        adapter = new BankAdapter(cardList, this);
        listView.setAdapter(adapter);
        refreshLayout.setHeaderView(new HeadRefreshView(this));
        refreshLayout.setCanLoadMore(false);
        params.put("memberId", MApplication.getInstance().getUser().getMemberId());
        refreshLayout.setRefreshListener(new BaseRefreshListener() {
            @Override
            public void refresh() {
                getCard();
            }

            @Override
            public void loadMore() {
            }
        });
    }

    @Event(value = R.id.add_bank_text)
    private void addBank(View view){
        Intent intent = new Intent(this,AddBankCardActivity.class);
        startActivity(intent);
    }

    /**
     * get comment list
     */
    private void getCard() {
        if (showLoadingDialog()) {
            CommonHttp.commonHttp("hzg/member/queryBank", params, callback);
        }
    }
}
